package persistencia;

import modelo.Facturas;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import static persistencia.DBManagerFacturas.conexion;

/**
 *
 * @author Antonio David
 */
public class MetodosFacturas {

    public Integer getIdFacturas(String n) throws java.sql.SQLException {
        Integer id = null;


        try {
            Statement s = conexion.createStatement();
            String con = "Select idFacturas from facturas where id = '" + n + "'";

            ResultSet rs = s.executeQuery(con);

            if (rs.next()) {
                id = rs.getInt("id");
            }

        } catch (SQLException ex) {
            System.err.println("ERROR. DBManager.modFacturas(). Error de SQL.");
            throw ex;
        }
        return id;
    }
    
     public ArrayList<Facturas> getFacturas() throws java.sql.SQLException {
        ArrayList<Facturas> alFacturas = new ArrayList<Facturas>();
        Facturas f;

        try {
            Statement s = conexion.createStatement();
            String sql = "select id_factura,id_cliente,id_albaran,id_articulo,nombre_articulo,precio_total from facturas";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                f = new Facturas(rs.getInt("id_factura"),rs.getInt("id_cliente"),rs.getInt("id_albaran"),rs.getInt("id_articulo"),
                                 rs.getString("nombre_articulo"),rs.getInt("precio_total"));
                alFacturas.add(f);

            }
        } catch (SQLException ex) {
            System.err.println("ERROR. DBManager.getClientes(). Error de SQL.");
            throw ex;
        }
        return alFacturas;
    }

    public void BuscarFacturas(String e) throws SQLException {
        Statement s = conexion.createStatement();
        String sql = "select * from facturas where id_facturas ='" + e + "' ";
    }

    public void addFacturas(Facturas ic, Facturas ia, Facturas iar, Facturas nar,Facturas ft) throws java.sql.SQLException {
        try {
            Statement s = conexion.createStatement();
            String sql = "insert into facturas (FACTURA) values ('" + ic.getId_cliente() + ia.getId_albaran() + iar.getId_articulo() 
                    + nar.getNombre_articulo() + ft.getPrecio_total() + "')";
            System.out.println(sql);
            s.executeUpdate(sql);
        } catch (SQLException ex) {
            System.err.println("ERROR. DBManager.addFacturas(). Error de SQL.");
            throw ex;
        }
    }

    public void modFacturas(Facturas id_f,Facturas id_c, Facturas id_al, Facturas id_ar, Facturas nom_art, Facturas pt) throws java.sql.SQLException {
        try {
            Statement s = conexion.createStatement();

            //Integer id = this.getIdPaciente(old.getId());
            //String sql = "UPDATE marcas SET nombre=('" + news.getNombre() + "') where= ('" + old.getNombre() + "')";
            String sql = "UPDATE facturas SET id_cliente='" + id_c.getId_cliente()+ 
                                            "'id_albaran='" + id_al.getId_albaran()+ 
                                            "'id_articulo='" + id_ar.getId_articulo()+ 
                                            "'nombre_articulo='" + nom_art.getNombre_articulo()+ 
                                            "'precio_total='" + pt.getPrecio_total()+ "' where id_factura='" + id_f.toString()+ "'";
            System.out.println(sql);
            s.executeUpdate(sql);
        } catch (SQLException ex) {
            System.err.println("ERROR. DBManager.modFacturas(). Error de SQL.");
            throw ex;
        }
    }

    public void delFacturas(Facturas d) throws java.sql.SQLException {
        try {
            Statement s = conexion.createStatement();
            String sql = "delete from facturas where id_facturas='" + d.getId_facturas() + "'";
            System.out.println(sql);
            s.executeUpdate(sql);
        } catch (SQLException ex) {
            System.err.println("ERROR. DBManager.delFacturas().Error de SQL.");
            throw ex;
        }
    }

    public ResultSet ConsultaFacturas(int id_facturas) throws java.sql.SQLException {
        ResultSet rs = null;
        try {
            Statement s = conexion.createStatement();
            String sql = "Select id_facturas, cliente, id_albaran, precio_total, from facturas where id_factura ";        
            System.out.println(sql);
            rs = s.executeQuery(sql);
        } catch (SQLException ex) {
            System.err.println("ERROR. DBManager.addFacturas(). Error de SQL.");
            throw ex;
        }
        return rs;
    }

    

}
